package com.zjl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjl.dto.SetMealDto;
import com.zjl.mapper.SetMealMapper;
import com.zjl.pojo.SetMeal;
import com.zjl.pojo.SetMealDish;
import com.zjl.service.SetMealDishService;
import com.zjl.service.SetMealService;
import com.zjl.utils.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, SetMeal> implements SetMealService {

    @Autowired
    private SetMealDishService setMealDishService;

    /**
     * 保存套餐
     * @param setMealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetMealDto setMealDto) {
        this.save(setMealDto);
        List<SetMealDish> setMealDishList = setMealDto.getSetMealDishList();
        setMealDishList = setMealDishList.stream().peek(item -> item.setSetMealId(setMealDto.getId())).collect(Collectors.toList());
        setMealDishService.saveBatch(setMealDishList);
    }

    /**
     * 查询回显
     * @param id
     * @return
     */
    @Override
    public SetMealDto getByIdWithDish(Long id) {
        SetMeal setMeal = this.getById(id);
        SetMealDto setMealDto = new SetMealDto();
        BeanUtils.copyProperties(setMeal, setMealDto);

        LambdaQueryWrapper<SetMealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetMealDish::getSetMealId, setMeal.getId());
        List<SetMealDish> setSetMealDishList = setMealDishService.list(queryWrapper);
        setMealDto.setSetMealDishList(setSetMealDishList);
        return setMealDto;
    }

    /**
     * 更新套餐
     * @param setMealDto
     */
    @Override
    @Transactional
    public void updateWithSetMealDish(SetMealDto setMealDto) {
        this.updateById(setMealDto);
        LambdaQueryWrapper<SetMealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetMealDish::getSetMealId, setMealDto.getId());
        setMealDishService.remove(queryWrapper);

        List<SetMealDish> setMealDishList = setMealDto.getSetMealDishList();
        setMealDishList = setMealDishList.stream().map(item -> {
            item.setSetMealId(setMealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setMealDishService.saveBatch(setMealDishList);
    }

    @Override
    @Transactional
    public void removeWithSetMealDish(List<Long> ids) {
        LambdaQueryWrapper<SetMeal> setMealQueryWrapper = new LambdaQueryWrapper<>();
        setMealQueryWrapper.in(SetMeal::getId, ids);
        setMealQueryWrapper.eq(SetMeal::getStatus, 1);
        int count = (int) this.count(setMealQueryWrapper);
        if(count > 0) {
            throw new CustomException("套餐正在售卖，不能删除");
        }
        this.removeByIds(ids);

        LambdaQueryWrapper<SetMealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetMealDish::getSetMealId, ids);
        setMealDishService.remove(queryWrapper);
    }
}
